//
//  NRWMineVC.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/11.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWMineVC.h"

#import "NRWMineSettingVC.h"
#import "NRWMineInfoCell.h"
#import "NRWCenterMenuCell.h"
#import "NRWBottomMenuCell.h"
#import "AboutUsVC.h"
#import "FeedbackVC.h"
#import "NRWMyCollectionVC.h"
#import "NRWMySubscriptionVC.h"
#import "NRWLookupHistoryVC.h"
#import "NRWMyMessageVC.h"

#import "NRWLoginView.h"
#import "NRWUserInfoModel.h"
#import "NRWCarShareTool.h"
#import "WXApi.h"

@interface NRWMineVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, strong) NRWUserInfoModel *loginModel;

@end

@implementation NRWMineVC

#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationView setTitle:@"我的"];
    [self.navigationView setRightTitle:@"" image:@"mine_setting"];
    [self Initcollectionview];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessful:) name:userLoginSuccess object:nil];
    self.loginModel = [NRWUserInfoModel shared];

}

-(void)loginSuccessful:(NSNotification *)nofit{
    NSString *type = nofit.object;
    if ([type isEqualToString:kMineLogin]) {
        [self.collectionview reloadData];
    }
}
-(void)navigationViewRightItemDidClick{
    NRWMineSettingVC *settingVC = [[NRWMineSettingVC alloc] init];
    [self.navigationController pushViewController:settingVC animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.collectionview reloadData];

}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
#pragma mark - setupUI
-(void)Initcollectionview{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing      = 0;
    layout.minimumInteritemSpacing = 0;
    _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0,getNavHeight(), SCREEN_WIDTH(), SCREEN_HEIGHT()) collectionViewLayout:layout];
    _collectionview.backgroundColor = backColor();
    [_collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collectionview registerNib:[UINib nibWithNibName:@"NRWMineInfoCell" bundle:nil] forCellWithReuseIdentifier:@"NRWMineInfoCell"];
    [_collectionview registerNib:[UINib nibWithNibName:@"NRWCenterMenuCell" bundle:nil] forCellWithReuseIdentifier:@"NRWCenterMenuCell"];
    [_collectionview registerNib:[UINib nibWithNibName:@"NRWBottomMenuCell" bundle:nil] forCellWithReuseIdentifier:@"NRWBottomMenuCell"];
    if (@available(iOS 11.0, *)) {
        _collectionview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    [self.view addSubview:_collectionview];
    [self.view bringSubviewToFront:self.navigationView];
}
#pragma mark - CollectionDelegateAndSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 4;
    }else{
        return 4;
    }
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerview =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerview.backgroundColor = backColor();
        return headerview;
    }
    return nil;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NRWMineInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NRWMineInfoCell" forIndexPath:indexPath];
        if (self.loginModel.isLogin == YES) {
            cell.phoneNoStr = self.loginModel.userMobile;
        }
        cell.isLogin = self.loginModel.isLogin;
        return cell;
    }else if (indexPath.section == 1){
        NRWCenterMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NRWCenterMenuCell" forIndexPath:indexPath];
        cell.index = indexPath;
        return cell;
    }else{
        NRWBottomMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NRWBottomMenuCell" forIndexPath:indexPath];
        cell.index = indexPath;
        return cell;
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH(), 70);
    }else if (indexPath.section == 1){
        return CGSizeMake(SCREEN_WIDTH()/4, 85);
    }else{
//        if (indexPath.row == 0) {
//
//            return CGSizeMake(SCREEN_WIDTH(), 0.0001);
//
//        }
        return CGSizeMake(SCREEN_WIDTH(), 45);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH(), 4);
    }else{
        return CGSizeMake(SCREEN_WIDTH(), 10);
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.section == 0) {
        if (self.loginModel.isLogin == NO) {
            NRWLoginView *loginView = [[NRWLoginView alloc] init];
            loginView.type = kMineLogin;
            [loginView show];
            return;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            if (self.loginModel.isLogin == NO) {
                NRWLoginView *loginView = [[NRWLoginView alloc] init];
                loginView.type = kMineLogin;
                [loginView show];
                return;
            }
            NRWMyCollectionVC *collectionVC = [[NRWMyCollectionVC alloc] init];
            [self.navigationController pushViewController:collectionVC animated:YES];
        }else if (indexPath.row == 1){
            if (self.loginModel.isLogin == NO) {
                NRWLoginView *loginView = [[NRWLoginView alloc] init];
                loginView.type = kMineLogin;
                [loginView show];
                return;
            }
            NRWMySubscriptionVC *subscriptionVC = [[NRWMySubscriptionVC alloc] init];
            [self.navigationController pushViewController:subscriptionVC animated:YES];

        }else if (indexPath.row == 2){
            NRWLookupHistoryVC *lookupVC = [[NRWLookupHistoryVC alloc] init];
            [self.navigationController pushViewController:lookupVC animated:YES];
        }else if (indexPath.row == 3){
            if (self.loginModel.isLogin == NO) {
                NRWLoginView *loginView = [[NRWLoginView alloc] init];
                loginView.type = kMineLogin;
                [loginView show];
                return;
            }
            NRWMyMessageVC *messageVC = [[NRWMyMessageVC alloc] init];
            [self.navigationController pushViewController:messageVC animated:YES];
        }
        
    }else{
        if (indexPath.row == 0) {
            [self shareApp];
        }else if (indexPath.row == 1){
            [self callPhoneWithPhoneNum:@"4001501999"];
        }else if (indexPath.row == 2){
          
            FeedBackVC *feedBackVC = [[FeedBackVC alloc] init];
            [self.navigationController pushViewController:feedBackVC animated:YES];
        }else if (indexPath.row == 3){
            AboutUsVC *aboutUsVC = [[AboutUsVC alloc] init];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
    }

}
-(void)shareApp{
    if ([WXApi isWXAppInstalled]) {
        //显示弹出框列表选择
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"分享"
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                       handler:^(UIAlertAction * action) {
                                                           
                                                           
                                                       }];
        UIAlertAction* wxcircle = [UIAlertAction actionWithTitle:@"朋友圈" style:UIAlertActionStyleDestructive
                                                         handler:^(UIAlertAction * action) {
                                                             [NRWCarShareTool shareCarDetailWithCarDetailModelCustomerTitle:@"一站式汽车服务平台正式上线拉,买车卖车尽在启辕车商城" url:@"http://a.app.qq.com/o/simple.jsp?pkgname=com.x.client" shareType:ShareTypeWXCircle];
                                                         }];
        UIAlertAction* weChat = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDestructive
                                                       handler:^(UIAlertAction * action) {
                                                           [NRWCarShareTool shareCarDetailWithCarDetailModelCustomerTitle:@"一站式汽车服务平台正式上线拉,买车卖车尽在启辕车商城" url:@"http://a.app.qq.com/o/simple.jsp?pkgname=com.x.client" shareType:ShareTypeWeiXin];
                                                       }];
        [alert addAction:weChat];
        [alert addAction:wxcircle];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        [NRWMessageHUD showMessage:@"暂时无法分享" forTime:1.5 withRemindId:remindWarning];
    }
}

#pragma mark - privateMethod

#pragma mark - publicMethod

#pragma mark - DelegateMethod

#pragma mark - touchEventAction

#pragma mark - setter

#pragma mark - getter

@end
